#!/bin/bash

. $TEST_ROOT_DIR/run-test-common

# This test uses a private dataset which is not part of the CNTK repository itself
# We use the dataset from an external location specified using an environment variable
if [[ "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" == "" || ! -d "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" ]]; then
  echo 'This test uses external data that is not part of the CNTK repository. Environment variable CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY must be set to point to the external test data location'
  exit 1
fi

if [ "$OS" == "Windows_NT" ]; then
    DataSourceDir=`cygpath -au $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY`/ModelExport
else
    DataSourceDir=$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY/ModelExport
fi

# Copy the test data to the test run directory
DataDir=$TEST_RUN_DIR/TestData
echo DataDir : $DataDir
mkdir $DataDir
cp -R $DataSourceDir/* $DataDir || exit $?

# cntkrun <CNTK config file name> <additional CNTK args>
cntkrun 1.cntk
ExitCode=$?

# Compare the exported dbn file against the base dbn
if [ $ExitCode -eq 0 ]; then
    BaseFileName=1_base_$TEST_DEVICE.dbn
    echo Comparing exported file to base file
    cmp -s $DataDir/$BaseFileName $DataDir/1_export.dbn
    CopyCode=$?
    if [ $CopyCode -ne 0 ]; then
        echo Files are different
    fi
    ExitCode=$CopyCode
fi

# Delete the test data
rm -rf $DataDir

exit $ExitCode